import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

//加载转状态
enum ViewState {
  error, //加载错误
  normal, //正常加载
  loading, //loading的状态
  empty, //无数据的状态
}

class BaseViewModel extends ChangeNotifier {
  late bool disposed = false;
  late BuildContext context;
  BaseViewModel();
  ViewState _state = ViewState.loading;

  ViewState get state => _state;

  requestData({Function(dynamic)? onSuccess, Function(dynamic)? onError}) {}

  void setState(ViewState viewState) {
    _state = viewState;
    notifyListeners();
  }

  void initState(ViewState viewState) {
    _state = viewState;
  }

  void notifyPageLoading() {
    if (_state == ViewState.loading) return;
    _state = ViewState.loading;
    notifyListeners();
  }

  void notifyPageError() {
    if (_state == ViewState.error) return;
    _state = ViewState.error;
    notifyListeners();
  }

  void notifyPageNormal() {
    if (_state == ViewState.normal) return;
    _state = ViewState.normal;
    notifyListeners();
  }

  void notifyPageEmpty() {
    if (_state == ViewState.empty) return;
    _state = ViewState.empty;
    notifyListeners();
  }

  @override
  void dispose() {
    super.dispose();
    disposed = true;
  }

  @override
  void notifyListeners() {
    if (!disposed) {
      super.notifyListeners();
    }
  }
}

// class BasePage<T extends BaseViewModel> extends StatefulWidget {
//   final Widget Function(BuildContext context, T model, Widget? child) builder;
//   final T viewModel;
//   final Widget? child;
//   final Function(T)? onModelReady;

//   const BasePage({super.key, required this.viewModel, required this.builder, this.child, this.onModelReady});

//   @override
//   State<BasePage> createState() => _BasePageState();
// }

// class _BasePageState<T extends BaseViewModel> extends State<BasePage<T>> {
//   late T viewModel;

//   @override
//   void initState() {
//     viewModel = widget.viewModel;
//     if (widget.onModelReady != null) {
//         widget.onModelReady!(widget.viewModel);
//     }
//     super.initState();
//   }

//   @override
//   Widget build(BuildContext context) {
//     return ChangeNotifierProvider<T>(
//       child: Consumer<T>(
//         builder: widget.builder,
//         child: widget.child,
//       ),
//       create: (BuildContext context) {
//         return viewModel;
//       },
//     );
//   }
// }

class BasePage<T extends BaseViewModel> extends StatefulWidget {
  final Widget Function(BuildContext context, T viewModel, Widget? child) builder;

  final T viewModel;
  final Widget? child;
  final Function(T)? onModelReady;

  const BasePage({super.key, required this.viewModel, required this.builder, this.child, this.onModelReady});

  @override
  State<BasePage<T>> createState() => _BasePageState<T>();
}

class _BasePageState<T extends BaseViewModel> extends State<BasePage<T>> {
  late T viewModel;

  @override
  void initState() {
    viewModel = widget.viewModel;
    if (widget.onModelReady != null) {
      widget.onModelReady!(viewModel);
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<T>(
      child: Consumer<T>(
        builder: widget.builder,
        child: widget.child,
      ),
      create: (BuildContext context) {
        viewModel.context = context;
        return viewModel;
      },
    );
  }
}
